#include "rsktcn01.h"
#include "usart.h"

static void RSKTCN01_Prase(uint8_t *buf,uint8_t len);
static void RSKTCN01_Data_Ask(void);
static void RSKTCN01_Set_Temp(short temp);
static void RSKTCN01_Set_Hemp(short hemp);
static void RSKTCN01_Set_Mode(uint8_t mode);
static T_RSKTCN01_DATA RSKTCN01_Get_Data(void);
static void RSKTCN01_Process(uint8_t mode);
	
 T_RSKTCN01_DATA T_rsktcn01_data = {0,0,0,0};
 T_RSKTCN01_CMD  T_rsktcn01_cmd = {
	{0X01,0X03,0X00,0X01,0X00,0X01,0XD5,0XCA},
	{0X01,0X03,0X00,0X00,0X00,0X01,0X84,0X0A},
	{0X01,0X03,0X00,0X00,0X00,0X02,0XC4,0X0B},
	{0X01,0X03,0X00,0X71,0X00,0X01,0XD4,0X11},
	{0X01,0X06,0X00,0xB9,0X00,0X01,0X99,0XEF},
	{0X01,0X06,0X00,0XBA,0X00,0X01,0X69,0XEF},
	{0X01,0X06,0X00,0XBB,0X00,0X01,0X38,0X2F}
};

T_RSKTCN01_FUN Rsktcn01_fun = RSKTCN01_FucGroundInit;
#undef RSKTCN01_FucGroundInit

void RSKTCN01_Usart6_It_Init(void)
{
	usart3_init(4800);
//	Rsktcn01_fun.RSKTCN01_Process = RSKTCN01_Process;
}

static void RSKTCN01_Prase(uint8_t *buf,uint8_t len)
{
	if(buf[0] == 0x01 && buf[1] == 0x03 && buf[2] == 0x04)
	{
		T_rsktcn01_data.hum   = (buf[3] * 256 + buf[4]) / 10.0f;
		T_rsktcn01_data.temp  = (buf[5] * 256 + buf[6]) / 10.0f;
	}
}




static void RSKTCN01_Data_Ask(void)
{
//	uint8_t Ask_buff[8] = {0x03,0X03,0X00,0X00,0X00,0X02,0XC5,0XE9};
	 uint8_t Ask_buff[8] = {0x01,0X03,0X00,0X00,0X00,0X02,0XC4,0X0B};
	send_data_dma_u3(Ask_buff, 8);
}

static void RSKTCN01_Set_Temp(short temp)
{
	send_data_dma_u6(T_rsktcn01_cmd.Temp_Buf, 8);
}

static void RSKTCN01_Set_Hemp(short hemp)
{
	send_data_dma_u6(T_rsktcn01_cmd.Hemp_Buf, 8);
}

static void RSKTCN01_Set_Mode(uint8_t mode)
{
	if(mode == COLD)
	{
		send_data_dma_u6(T_rsktcn01_cmd.cold_down_Buf, 8);
	}
	else if(mode == WARM)
	{
		send_data_dma_u6(T_rsktcn01_cmd.warm_up_Buf, 8);
	}
	else if(mode == OFF)
	{
		send_data_dma_u6(T_rsktcn01_cmd.turn_off_Buf, 8);
	}
}

static T_RSKTCN01_DATA RSKTCN01_Get_Data(void)
{
	return T_rsktcn01_data;
}

static void RSKTCN01_Process(uint8_t mode)
{
	if(mode == COLD)
	{
		send_data_dma_u6(T_rsktcn01_cmd.cold_down_Buf, 8);
	}
	else if(mode == WARM)
	{
		send_data_dma_u6(T_rsktcn01_cmd.warm_up_Buf, 8);
	}
	else if(mode == OFF)
	{
		send_data_dma_u6(T_rsktcn01_cmd.turn_off_Buf, 8);
	}
	else{
		RSKTCN01_Data_Ask();
	}
}



